﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Collections;
namespace DBA.App.Dictionary
{                   

    public class Dictionary
    {
        public static string strIndexFile = Application.StartupPath + @"\dict\en-vi.index";
        public static string strDictFile = Application.StartupPath + @"\dict\en-vi.dict";
        public static List<EntryWord> entryList;
        public static bool isloaded = false;
        public static int intIndex;

        public static int FindIndexEntry(List<EntryWord> list, String strWord)
        {
            //use binary search to locate index word
            int l = 0;                              
            int r = list.Count;                     
            int m = 0;
            while (l <= r)
            {
                m = (l + r) / 2;
                if (String.Compare(list[m].Text, strWord.ToLower()) == 0)
                    return m;
                if (String.Compare(list[m].Text, strWord.ToLower()) < 0)
                    l = m + 1;
                else
                    r = m - 1;
            }
            intIndex = m;
            return 0;           
        }

        public static int FindIndexEntry2(List<EntryWord> list, String strWord)
        {
            for (int i = 0; i < list.Count; i++ )
            {
               if( string.Compare(strWord, list[i].Text) == 0 )
                   return i;
            }               
            return 0;
        }
        

        /// <summary>
        /// Get a list of nearest words of the word that you want to lookup
        /// </summary>
        /// <param name="strWord">the word you lookup</param>
        /// <returns>list of words </returns>
        public static  ArrayList getNearestWord(String strWord)
        {
            
            System.Collections.ArrayList list = new System.Collections.ArrayList();
            int index;
            
            index=Dictionary.FindIndexEntry(entryList, strWord);
            if (index == 0)
            {
                index = intIndex;
            }
            
            for (int i = index; i < index + 50; i++) 
            {
                list.Add(entryList[i].Text);
            }
            return list;
        }

        public static void ReLoadIndexFile()
        {
            if (entryList != null)
                entryList.Clear();
            LoadIndexFile();
        }
        public static void LoadIndexFile()        
        {                                                    
            entryList = new List<EntryWord>();
            using (System.IO.StreamReader sr = System.IO.File.OpenText(strIndexFile))
            {
                String str = "";
                int count = 0;
                while ((str = sr.ReadLine()) != null)
                {
                    String[] stra = str.Split(new char[] { '\t' });
                    EntryWord t = new EntryWord();
                    t.Index = count++;
                    t.Text =  stra[0];
                    t.strOffSet = stra[1];
                    t.strSize = stra[2];
                    entryList.Add(t);
                }                                
            }
            isloaded = true;
        }      
    
        public static String LookUp(String strWord)
        {
            int intIndex;           //index entrylist
            long longOffSet;        //offset 
            long longSize;          //size
            FileStream fs = null;   //read file
            byte[] bt;              //save byte reading from file
            
            //  find index of entryword
            intIndex = FindIndexEntry2(entryList, strWord);
            if (intIndex == 0)
            {                      
                return "No defination";
            }
                
            //  find offset and size from entry list
            longOffSet = Convert.ToDecimal(entryList[intIndex].strOffSet);
            longSize = Convert.ToDecimal(entryList[intIndex].strSize);

            //  read file to get meaning string by offset and size          
            fs = new FileStream(strDictFile, FileMode.Open, FileAccess.Read);
            bt = new byte[longSize];
            fs.Seek((long)longOffSet, SeekOrigin.Begin);
            fs.Read(bt, 0, (int)longSize);
            fs.Close();
            return System.Text.ASCIIEncoding.UTF8.GetString(bt);

        }

       

        
    }
}
